package org.javabasics.thread;

/**
 * Created by LUOLIANG on 2016/9/25.
 * 测试让一个线程等待另一个线程完成的的方法join()
 */
public class JoinThread extends Thread {

    public JoinThread(String name) {
        super(name);
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(getName() + " " + i);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        //启动新线程    和main线程并发执行
        new JoinThread("新线程").start();
        for (int i = 0; i < 100; i++) {
            if (i == 20) {
                JoinThread jt = new JoinThread("被join的线程");
                jt.start();
//                main线程调用了jt线程的join方法，main线程必须等到jt执行完成后才会向下执行
                jt.join();
            }
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
    }
}
